<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <!--[if lt IE 9]>
    <script type="text/javascript" src="lib/html5shiv.js"></script>
    <script type="text/javascript" src="lib/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="static/h-ui/css/H-ui.min.css" />
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/H-ui.admin.css" />
    <link rel="stylesheet" type="text/css" href="lib/Hui-iconfont/1.0.8/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/skin/default/skin.css" id="skin" />
    <link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/style.css" />
    <!--[if IE 6]>
    <script type="text/javascript" src="lib/DD_belatedPNG_0.0.8a-min.js" ></script>
    <script>DD_belatedPNG.fix('*');</script>
    <![endif]-->
    <title>优惠方案管理</title>
</head>
<style>
    .table>tbody>tr>td{
        text-align:center;
    }
</style>
<body>

<div>
    <nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 优惠方案管理 <span class="c-gray en">&gt;</span> 商品优惠方案管理 <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a></nav>
    <form class="page-container">
        <div class="cl pd-5 bg-1 bk-gray mt-20">
            <span class="l">
                <span style="display: none;" id="preferential">优惠券:<span id="preferentialName"></span></span>
                <span style="display: none;" id="item">商品:<span id="itemName"></span></span>
                <a class="btn btn-primary radius" onclick="add('添加商品优惠方案','preferential-item-edit' + (preferentialId!=null||itemId!=null?'?':'') + (preferentialId!=null?'preferentialId='+preferentialId:'') + (itemId!=null?'itemId='+itemId:''),900,600)" href="javascript:;"><i class="Hui-iconfont">&#xe600;</i> 添加商品优惠方案</a>
                <%--<a href="javascript:;" onclick="delCatAttrs()" class="btn btn-danger radius"><i class="Hui-iconfont">&#xe6e2;</i> 批量删除</a>--%>

            </span>
        </div>
        <div class="mt-20">
            <div class="mt-20" style="margin-bottom: 70px">
                <table class="table table-border table-bordered table-bg table-hover table-sort" width="100%">
                    <thead>
                    <tr class="text-c">
                        <th width="15"><input type="checkbox" value="" name=""></th>
                        <th width="30">ID</th>
                        <th width="80">优惠方案</th>
                        <th width="80">商品</th>
                        <th width="50">操作</th>
                    </tr>
                    </thead>
                </table>
            </div>
        </div>
    </form>
</div>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="lib/datatables/dataTables.colReorder.min.js"></script>
<script type="text/javascript" src="lib/laypage/1.2/laypage.js"></script>
<script type="text/javascript" src="lib/common.js"></script>
<script type="text/javascript">
    var sEcho = 0
    function loadData(sSource,aoData, fnCallback){
        var pageNo = 0;
        var pageSize = 10;
        var start = 0;
        var length = 10;
        for(var i = 0;i<aoData.length;i++){
            var item = aoData[i];
            if(item.name == "start"){
                start = item.value;
            }else if(item.name == "length"){
                length = item.value;
            }
        }
        pageSize = length;
        pageNo = parseInt(start / pageSize);
        $.ajax({
            url : "/sys/preferential-item/page",//这个就是请求地址对应sAjaxSource
            data : {
                preferentialId:preferentialId,
                itemId:itemId,
                pageNo:pageNo,
                pageSize:pageSize
            },//这个是把datatable的一些基本数据传给后台,比如起始位置,每页显示的行数
            type : 'GET',
            dataType : 'json',
            async : false,
            success : function(result) {
                var res = {}
                res.sEcho = sEcho + 1
                sEcho += 1
                res.iTotalRecords = result.totalCount
                // res.iTotalDisplayRecords = (result.hasPre == true?result.prePage * result.pageParam.pageSize:0) + result.result.length
                res.iTotalDisplayRecords = result.totalCount
                res.aaData = result.result
                fnCallback(res);//把返回的数据传给这个方法就可以了,datatable会自动绑定数据的
            },
            error : function(msg) {
            }
        });
    }
    //获取url中的参数
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]); return null; //返回参数值
    }
    var preferentialId = null
    var itemId = null
    /*datatables配置*/
    $(document).ready(function () {
        preferentialId = getUrlParam("preferentialId")
        if(preferentialId != null){
            $.ajax({
                url:"/sys/preferential/"+preferentialId,
                method:"GET",
                success: function(result){
                    $("#preferential").show()
                    $("#preferentialName").html(result.content)
                }
            })
        }
        itemId = getUrlParam("itemId")
        if(itemId != null){
            $.ajax({
                url:"/item/"+itemId,
                method:"GET",
                success: function(result){
                    $("#item").show()
                    $("#itemName").html(result.result.title)
                }
            })
        }
        $('.table').DataTable({
            "serverSide": true,//开启服务器模式:启用服务器分页
            paging : true,//是否分页
            pagingType : "full_numbers",//除首页、上一页、下一页、末页四个按钮还有页数按钮
            "processing": true,//加载显示提示
            "fnServerData": loadData, // 获取数据的处理函数
            "aoColumns": [
                { "mData.": null,
                    render : function(data,type, row, meta) {
                        return "<input name=\"checkbox\" value=\""+row.id+"\" type=\"checkbox\" value=\"\">";
                    }
                },
                { "mData": "id"},
                { "mData": "preferentialContent"},
                { "mData": "itemTile"},
                {
                    "mData": null,
                    render: function (data, type, row, meta) {
                        if(data.status == 0){
                            return "<a style=\"text-decoration:none\" class=\"ml-5\" onclick=\"able("+row.id+",'启用')\" href=\"javascript:;\" title=\"启用\"><i class=\"Hui-iconfont\">&#xe6e1;</i></a>";
                        }else{
                            return "<a style=\"text-decoration:none\" class=\"ml-5\" onclick=\"able("+row.id+",'禁用')\" href=\"javascript:;\" title=\"禁用\"><i class=\"Hui-iconfont\">&#xe631;</i></a>";
                        }
                    }
                }
            ],
            "aaSorting": [[ 1, "asc" ]],//默认第几个排序
            "bStateSave": false,//状态保存
            "aoColumnDefs": [
                {"orderable":false,"aTargets":[0,2,3,4]}// 制定列不参与排序
            ],
            language: {
                url: '/lib/datatables/Chinese.json'
            },
            colReorder: true
        });

    });

    function add(title,url,w,h){
        layer_show(title,url,w,h);
    }

    var id,expressName,sortOrder;
    function edit(title,url,w,h){
        var table = $('.table').DataTable();
        $('.table tbody').on( 'click', 'tr', function () {
            id = table.row(this).data().id;
            expressName = table.row(this).data().expressName;
            sortOrder = table.row(this).data().sortOrder;
        });
        layer_show(title,url,w,h);
    }

    function able(id,title){
        layer.confirm('确认要'+title+'ID为\''+id+'\'的数据吗？',{icon:0},function(index){
            $.ajax({
                type: 'get',
                url: '/sys/preferential-item/open/'+id,
                dataType: 'json',
                success: function(data){
                    if(data.success!=1){
                        layer.alert(data.message,{title: '错误信息',icon: 2});
                        return;
                    }
                    refresh();
                    layer.msg('已禁用!',{icon:1,time:1000});
                },
                error:function(XMLHttpRequest) {
                    layer.alert('数据处理失败! 错误码:'+XMLHttpRequest.status,{title: '错误信息',icon: 2});
                },
            });
        });
    }

    function msgSuccess(content){
        layer.msg(content, {icon: 1,time:3000});
    }
</script>
</body>
</html>
